package com.zelin.service;

import com.zelin.dao.ClassesDao;
import com.zelin.dao.StudentDao;
import com.zelin.pojo.Classes;
import com.zelin.pojo.PageResult;
import com.zelin.pojo.Result;
import com.zelin.pojo.Student;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;


@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassesDao classesDao;
    //1.列表学生
    public List<Student> findAll() {
        List<Student> studentList = studentDao.findAll();
        for (Student student : studentList) {
             Classes classes = classesDao.findById(student.getCid()).get();
             student.setCname(classes.getCname());
            }
        return studentList;
    }
    //2. 分页查询
    public PageResult findByPage(int page, int pageSize) {
        //2.1 得到分页对象
        Page<Student> page1 = studentDao.findAll(PageRequest.of(page-1,pageSize));
        return new PageResult(page1.getTotalElements(),
                page1.getTotalPages(),
                page1.getContent());
    }
    //3.条件查询带分页
    public PageResult search(int page, int pageSize, Student student) {
        System.out.println("student = " + student);
        //3.1)根据条件得到对象
        Specification<Student> spec = createSpecification(student);
        //3.2)查询得到分页对象
        Page<Student> studentPage = studentDao.findAll(spec,PageRequest.of(page-1,pageSize));
        List<Student> content = studentPage.getContent();
        for (Student stud : content) {
            Classes classes = classesDao.findById(stud.getCid() ).get();
            stud.setCname(classes.getCname());
        }
        //3.3)构造PageResult并返回
        return new PageResult(studentPage.getTotalElements(),
                studentPage.getTotalPages(),
                content);
    }
    //得到查询条件对象
    private Specification<Student> createSpecification(Student student) {
        return new Specification<Student>() {
            @Override
            public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                //1.定义查询对象集合(将所有的查询条件放置其中)
                List<Predicate> predicateList = new ArrayList<>();
                //2.根据查询条件对象student向集合中添加具体的查询条件
                if(student != null){
                    if(StringUtils.isNotBlank(student.getSname())){     //处理学生姓名条件
                        predicateList.add(cb.like(root.get("sname").as(String.class),"%" + student.getSname() + "%"));
                    }
                    if(StringUtils.isNotBlank(student.getAddr())){
                        predicateList.add(cb.like(root.get("addr").as(String.class),"%" + student.getAddr() + "%"));
                    }
                    if(student.getCid() != 0){
                        predicateList.add(cb.equal(root.get("cid").as(int.class),student.getCid()));
                    }
                }
                //3.根据集合大小定义数组(空数组)
                Predicate[] arr = new Predicate[predicateList.size()];
                //4.将集合中的数据存放到上面的空数组arr中
                Predicate[] predicates = predicateList.toArray(arr);
                return cb.and(predicates);
            }
        };
    }
    //4. 删除学生
    public void dele(Integer[] ids) {
        for (int id : ids) {
            studentDao.deleteById(id);
        }

    }
    //5.添加学生
    public void add(Student student) {
        studentDao.save(student);
    }
     //6. 修改学生
    public void update(Student student) {
        studentDao.save(student);
    }
}
